package jsyf.oa.service.wechat.service_company;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.wechat.service_company.TokenDao;
import jsyf.oa.facade.wechat.config.WeChatConst;
import jsyf.oa.facade.wechat.service_company.EnterpriseMenuFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class EnterpriseMenuService implements EnterpriseMenuFacade {

    @Autowired
    private TokenDao tokenDao;

    @Override
    public JSONObject createMenu(String jsonData) {

        JSONObject tokenObject = tokenDao.getToken();
        String token = tokenObject.getString("accessToken");
        // 拼装接口url
        String url = WeChatConst.create_enterprise_menu.replace("ACCESS_TOKEN", token);
        url = url.replace("AGENTID",  WeChatConst.agentId);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "POST", jsonData);

        return jsonObject;
    }

    @Override
    public JSONObject getMenu() {

        JSONObject tokenObject = tokenDao.getToken();
        String token = tokenObject.getString("accessToken");
        // 拼装接口url
        String url = WeChatConst.get_enterprise_menu.replace("ACCESS_TOKEN", token);
        url = url.replace("AGENTID",  WeChatConst.agentId);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "GET", null);

        return jsonObject;
    }

    @Override
    public JSONObject deleteMenu() {

        JSONObject tokenObject = tokenDao.getToken();
        String token = tokenObject.getString("accessToken");
        // 拼装接口url
        String url = WeChatConst.delete_enterprise_menu.replace("ACCESS_TOKEN", token);
        url = url.replace("AGENTID",  WeChatConst.agentId);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "GET", null);

        return jsonObject;
    }
}